<?php
namespace Common;
use JPush\Model as M;
use JPush\Client;
use JPush\Exceptions\JPushException;


/********************************************
 *****************JPush消息推送***************
 ********************************************/

class Jpush
{
   /* private $userKey = '8791abbec9b53bf0a850b27a';
    private $userSecret = '75a551d64f9be10c39c93395';
    private $shopKey = 'e2cf69d75da2017ceb48890e';
    private $shopSecret = '4cb28508c11b987d5216717e'; */
    private $userKey = '78cf4b416875e1642db19f95';
    private $userSecret = '52af3dd0fbcb34e6a9d089a1';
    private $shopKey = '39237f0f8dc6d4961ff43065';
    private $shopSecret = '8178deccb1093fcbff537bd6';
    private $client;

    public function __construct($type='user')
    {
        if($type=='user'){
            $client = new Client($this->userKey, $this->userSecret);
        }else if($type =='shop'){
            $client = new Client($this->shopKey, $this->shopSecret);
        }else{
            echo json_encode(['status'=>203,'msg'=>'客户发不存在']);
            exit();
        }
        $this->client=$client;
    }

//给全部用户推送
    function pushAll($msg, $extra = [])
    {
        $client = $this->client;
        $response = $client->push()
            ->setPlatform('all')
            ->setAudience('all')
            ->iosNotification($msg, $extra)
            ->androidNotification($msg, $extra)
            ->send();
        return $response;
    }


// 推送给一个或多个标签tag
    function pushTag($msg, $tag = [], $extra = [])
    {
        $client = $this->client;
        $response = $client->push()
            ->setPlatform(array('ios', 'android'))
            ->addTag($tag)
            ->iosNotification($msg, $extra)
            ->androidNotification($msg, $extra)
            ->send();
        return $response;
    }

// 推送给一个或多个别名alias数组
    function pushAlias($msg, $alias, $extra = [])
    {
        if($alias==''||empty($alias)){
            return true;
            exit();
        }
        $client = $this->client;
        $array = array(
            "title" => "message title",
            "extras" => array("key" => "value1", "value" => "value2")
        );
        $response = $client->push()
            ->setPlatform(array('ios', 'android'))
            ->addAlias($alias)
            ->androidNotification($msg, $extra)
            ->iosNotification($msg, $extra)
            ->send();
        return $response;
    }
}